package com.xuecheng.search.controller;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.search.dto.SearchCourseParamDto;
import com.xuecheng.search.dto.SearchPageResultDto;
import com.xuecheng.search.po.CourseIndex;
import com.xuecheng.search.service.CourseSearchService;
import com.xuecheng.search.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author 26455
 * @version 1.0
 * @description TODO
 * @date 2023/2/21 15:56
 */
@Api(value = "课程信息索引接口", tags = "课程信息索引接口")
@RestController
public class CourseIndexController {

    @Value("${elasticsearch.course.index}")
    private String courseIndexStore;
    @Autowired
    IndexService indexService;
    @Autowired
    CourseSearchService courseSearchService;

    @ApiOperation("添加课程索引")
    @PostMapping("/index/course")
    public Boolean add(@RequestBody CourseIndex courseIndex) {
        Long id = courseIndex.getId();
       if (id == null) {
            XueChengPlusException.cast("课程id为空");
        }
        Boolean result = indexService.addCourseIndex(courseIndexStore, String.valueOf(id), courseIndex);
        if (!result) {
            XueChengPlusException.cast("添加课程索引失败");
        }
        return result;
    }

    @ApiOperation("课程搜索列表")
    @GetMapping("/course/list")
    public PageResult<CourseIndex> list(PageParams pageParams, SearchCourseParamDto searchCourseParamDto) {

        SearchPageResultDto<CourseIndex> searchPageResultDto = courseSearchService.queryCoursePubIndex(pageParams, searchCourseParamDto);

        return searchPageResultDto;
    }
}
